package org.springblade.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.api.R;
import org.springblade.system.dto.ProjectManageDTO;
import org.springblade.system.entity.ProjectManage;
import org.springblade.system.mapper.ProjectManageMapper;
import org.springblade.system.service.IProjectManageService;
import org.springblade.system.utils.FilePathUtil;
import org.springblade.system.vo.ProjectManageVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author wzy
 * @Date 2021/12/13
 **/
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class ProjectManageServiceImpl extends BaseServiceImpl<ProjectManageMapper, ProjectManage> implements IProjectManageService {



	@Override
	public IPage<ProjectManageVO> managePage(ProjectManageDTO pageDto, IPage<ProjectManageVO> page){

		return page.setRecords(baseMapper.managePage(page,pageDto));
	}



	@Override
	public Long add(ProjectManage entity){
        this.save(entity);
		return entity.getId();
	}
	@Override
	public boolean edit(ProjectManage entity){
		return this.updateById(entity);
	}
   @Override
	public boolean removeByid(List<Long> ids){
		return this.deleteLogic(ids);
   }

   @Override
	public R<Object> upload(Integer type, MultipartFile file) throws IOException {
       // 文件名
	   String fileName = file.getOriginalFilename();
	   // 后缀名
	   String suffixName = fileName.substring(fileName.lastIndexOf("."));
	   String filePath = FilePathUtil.getRelativePath("upload",suffixName);
	   File dest = new File(FilePathUtil.FILE_STATIC_ROOT+ filePath);
	   // 创建父级目录
	   FilePathUtil.mkdirs(dest);
	   file.transferTo(dest);
        ProjectManage manage = new ProjectManage();
	   if (type.equals(0)){
		   manage.setProjectLogo(fileName);
		   this.save(manage);
		  return R.success("保存项目logo成功！");
	   }
	   if(type.equals(1)){
	   	manage.setLoginImage(fileName);
		   this.save(manage);
	   	return R.success("保存登录页图片成功！");
	   }
	   if (type.equals(2)){
	   	manage.setBrowserIcon(fileName);
		   this.save(manage);
	   	return R.success("保存浏览器图标成功！");
	   }
	   return R.fail("保存失败！") ;
   }
}
